(function ($, doc) {
    $.init({
        swipeBack:true,
        gestureConfig: {
            tap: true, //默认为true
            doubletap: true, //默认为false
            longtap: true, //默认为false
            swipe: true, //默认为true
            drag: true, //默认为true
            hold: true, //默认为false，不监听
            release: true //默认为false，不监听
        }
    });
    $.plusReady(function () {
        var url 	= apiurl + 'friends/message';
        mui.get(url, {uid:app.getState().user_id}, function (data) {
            renderlist(data);
        },'json')

        var msgTap = function (e) {
            var el  = this;
            var val = el.getAttribute('cid');
            plus.nativeUI.actionSheet({
                buttons: [ {
                    title: "删除"
                }]
            }, function(e) {
                if(e.index == 1) {
                    var url = apiurl + 'Friends/clearOne';
                    mui.get(url, {c_id:val}, function (res) {
                        if(res.status == 1){
                            document.querySelector('#app').removeChild(el);
                        }
                    },'json')
                }
                return true;
            })
        }

        $('#app').on('longtap', '.mui-table-view-cell', msgTap);

        mui("#app").on("tap", "a", function() {
            var s_id    = this.getAttribute('sid');
            var t 		= sysphone.getFriendlist();
            var fids 	= '';
            for (var k in t){
                fids += t[k]['f_id'] + ',';
            }
            fids += app.getState().user_id + ',';
            var url     = apiurl + 'friends/getProfileDetail';
            $.get(url, {s_id:s_id, u_id:app.getState().user_id,fids:fids}, function (data) {
                $.openWindow({
                    url: "profile_detail.html",
                    id: "profile_detail.html",
                    extras: {
                        s_id: s_id,
                        u_id: data.ProfileDetail.u_id,
                        name: data.ProfileDetail.name,//被评论人
                        avatar: data.ProfileDetail.avatar,
                        comment: data.ProfileDetail.comment,//评论
                        time: data.ProfileDetail.time,
                        images: data.ProfileDetail.images,
                        content: data.ProfileDetail.content,
                        zan: data.ProfileDetail.zan,
                        zan_sta: data.ProfileDetail.zan_sta
                    }
                })
            },'json')
        })

        mui("#header").on("tap", "#clearAll", function() {
            var url 	    = apiurl + 'friends/clearAll';
            var btnArray    = ['取消', '确定'];
            mui.confirm('清空所有消息？', '', btnArray, function(e) {
                if (e.index == 1) {
                    mui.get(url, {uid:app.getState().user_id}, function (res) {
                        if(res.status == 1){
                            document.querySelector('#app').style.marginTop = '20px';
                            document.querySelector('#app').innerText = '暂无消息';
                        }
                    },'json')
                } else {

                }
            })
        })

    })

    function renderlist(data){
        for(var i in data.message){
            if(data.message[i].f_header == '' || data.message[i].f_header == null){
                data.message[i].f_header = '../../static/images/1.png';
            }else{
                data.message[i].f_header = serverurl + data.message[i].f_header;
            }
        }
        document.querySelector("#app").innerHTML = template('list-template', {
            "flist": data
        });
    }
}(mui, document));